package com.museum.demo.Entity.Impl;

import com.museum.demo.Dao.UserEntityMapper;
import com.museum.demo.Entity.UserEntity;
import com.museum.demo.Service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.museum.demo.TokenManager.TokenCreate;


@Service
public class UserEntityImpl implements UserService {
    private static final Logger logger = LoggerFactory.getLogger(UserEntityImpl.class);

    @Autowired
    private UserEntityMapper userEntityMapper;

    @Override
    public boolean InsertUser(String Username, String Password, String Sex) {
        UserEntity user = new UserEntity();
        user.setUsername(Username);
        user.setUserpsw(Password);
        user.setUsersex(Sex);
        logger.info("=============================================================="+user.getUsername()+"==============================================================");
        try {
            int result = userEntityMapper.insertSelective(user);
            return true;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public int Login(String username, String password) {
        TokenCreate tokenCreate = new TokenCreate();
        UserEntity user = new UserEntity();
        user.setUsername(username);
        user.setUserpsw(password);
        userEntityMapper.insertSelective(user);

        int userid = userEntityMapper.SelectUseridByName(username);
        return userid;
    }

    @Override
    public String GetUsernameById(int userid) {
        String username;
        username = userEntityMapper.SelectUsernameById(userid);

        if(username != null){
            return username;
        }
        return null;
    }
}
